{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "https://www.pybroker.com/en/latest/notebooks/2.%20Backtesting%20a%20Strategy.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pybroker\n",
    "from pybroker import Strategy,StrategyConfig\n",
    "from pybroker.ext.data import AKShare"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "config = StrategyConfig(initial_cash=100000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "strategy = Strategy(AKShare(), '20150101', '20240703', config)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def buy_low(ctx):\n",
    "    # If shares were already purchased and are currently being held, then return.\n",
    "    if ctx.long_pos():\n",
    "        return\n",
    "    # If the latest close price is less than the previous day's low price,\n",
    "    # then place a buy order.\n",
    "    if ctx.bars >= 2 and ctx.close[-1] < ctx.low[-2]:\n",
    "        # Buy a number of shares that is equal to 25% the portfolio.\n",
    "        ctx.buy_shares = ctx.calc_target_shares(0.25)\n",
    "        # Set the limit price of the order.\n",
    "        ctx.buy_limit_price = ctx.close[-1] - 0.01\n",
    "        # Hold the position for 3 bars before liquidating (in this case, 3 days).\n",
    "        ctx.hold_bars = 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "strategy.add_execution(buy_low, ['000001'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def short_high(ctx):\n",
    "    # If shares were already shorted then return.\n",
    "    if ctx.short_pos():\n",
    "        return\n",
    "    # If the latest close price is more than the previous day's high price,\n",
    "    # then place a sell order.\n",
    "    if ctx.bars >= 2 and ctx.close[-1] > ctx.high[-2]:\n",
    "        # Short 100 shares.\n",
    "        ctx.sell_shares = 100\n",
    "        # Cover the shares after 2 bars (in this case, 2 days).\n",
    "        ctx.hold_bars = 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "strategy.add_execution(short_high, ['000002'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Backtesting: 2015-01-01 00:00:00 to 2024-07-03 00:00:00\n",
      "\n",
      "Loading bar data...\n",
      "Loaded bar data: 0:00:00 \n",
      "\n",
      "Test split: 2015-01-05 00:00:00 to 2024-07-03 00:00:00\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  0% (0 of 2309) |                       | Elapsed Time: 0:00:00 ETA:  --:--:--\n",
      " 10% (241 of 2309) |##                   | Elapsed Time: 0:00:00 ETA:   0:00:00\n",
      " 34% (791 of 2309) |#######              | Elapsed Time: 0:00:00 ETA:   0:00:00\n",
      " 52% (1201 of 2309) |##########          | Elapsed Time: 0:00:00 ETA:   0:00:00\n",
      " 69% (1611 of 2309) |#############       | Elapsed Time: 0:00:00 ETA:   0:00:00\n",
      " 87% (2021 of 2309) |#################   | Elapsed Time: 0:00:00 ETA:   0:00:00\n",
      "100% (2309 of 2309) |####################| Elapsed Time: 0:00:00 Time:  0:00:00\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Finished backtest: 0:00:04\n"
     ]
    }
   ],
   "source": [
    "result = strategy.backtest()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "\n",
    "chart = plt.subplot2grid((3, 2), (0, 0), rowspan=3, colspan=2)\n",
    "chart.plot(result.portfolio.index, result.portfolio['market_value'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>long_shares</th>\n",
       "      <th>short_shares</th>\n",
       "      <th>close</th>\n",
       "      <th>equity</th>\n",
       "      <th>market_value</th>\n",
       "      <th>margin</th>\n",
       "      <th>unrealized_pnl</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>symbol</th>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"11\" valign=\"top\">000001</th>\n",
       "      <th>2015-01-08</th>\n",
       "      <td>1614</td>\n",
       "      <td>0</td>\n",
       "      <td>14.96</td>\n",
       "      <td>24145.44</td>\n",
       "      <td>24145.44</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-451.92</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-09</th>\n",
       "      <td>1614</td>\n",
       "      <td>0</td>\n",
       "      <td>15.08</td>\n",
       "      <td>24339.12</td>\n",
       "      <td>24339.12</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-258.24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-12</th>\n",
       "      <td>1614</td>\n",
       "      <td>0</td>\n",
       "      <td>14.77</td>\n",
       "      <td>23838.78</td>\n",
       "      <td>23838.78</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-758.58</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-20</th>\n",
       "      <td>1793</td>\n",
       "      <td>0</td>\n",
       "      <td>13.83</td>\n",
       "      <td>24797.19</td>\n",
       "      <td>24797.19</td>\n",
       "      <td>0.0</td>\n",
       "      <td>35.86</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-21</th>\n",
       "      <td>1793</td>\n",
       "      <td>0</td>\n",
       "      <td>14.42</td>\n",
       "      <td>25855.06</td>\n",
       "      <td>25855.06</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1093.73</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-06-13</th>\n",
       "      <td>2175</td>\n",
       "      <td>0</td>\n",
       "      <td>10.80</td>\n",
       "      <td>23490.00</td>\n",
       "      <td>23490.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-87.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-06-14</th>\n",
       "      <td>2175</td>\n",
       "      <td>0</td>\n",
       "      <td>10.18</td>\n",
       "      <td>22141.50</td>\n",
       "      <td>22141.50</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-1435.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-06-21</th>\n",
       "      <td>2314</td>\n",
       "      <td>0</td>\n",
       "      <td>10.00</td>\n",
       "      <td>23140.00</td>\n",
       "      <td>23140.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-46.28</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-06-24</th>\n",
       "      <td>2314</td>\n",
       "      <td>0</td>\n",
       "      <td>9.98</td>\n",
       "      <td>23093.72</td>\n",
       "      <td>23093.72</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-92.56</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-06-25</th>\n",
       "      <td>2314</td>\n",
       "      <td>0</td>\n",
       "      <td>10.08</td>\n",
       "      <td>23325.12</td>\n",
       "      <td>23325.12</td>\n",
       "      <td>0.0</td>\n",
       "      <td>138.84</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>564 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                   long_shares  short_shares  close    equity  market_value  \\\n",
       "symbol date                                                                   \n",
       "000001 2015-01-08         1614             0  14.96  24145.44      24145.44   \n",
       "       2015-01-09         1614             0  15.08  24339.12      24339.12   \n",
       "       2015-01-12         1614             0  14.77  23838.78      23838.78   \n",
       "       2015-01-20         1793             0  13.83  24797.19      24797.19   \n",
       "       2015-01-21         1793             0  14.42  25855.06      25855.06   \n",
       "...                        ...           ...    ...       ...           ...   \n",
       "       2024-06-13         2175             0  10.80  23490.00      23490.00   \n",
       "       2024-06-14         2175             0  10.18  22141.50      22141.50   \n",
       "       2024-06-21         2314             0  10.00  23140.00      23140.00   \n",
       "       2024-06-24         2314             0   9.98  23093.72      23093.72   \n",
       "       2024-06-25         2314             0  10.08  23325.12      23325.12   \n",
       "\n",
       "                   margin  unrealized_pnl  \n",
       "symbol date                                \n",
       "000001 2015-01-08     0.0         -451.92  \n",
       "       2015-01-09     0.0         -258.24  \n",
       "       2015-01-12     0.0         -758.58  \n",
       "       2015-01-20     0.0           35.86  \n",
       "       2015-01-21     0.0         1093.73  \n",
       "...                   ...             ...  \n",
       "       2024-06-13     0.0          -87.00  \n",
       "       2024-06-14     0.0        -1435.50  \n",
       "       2024-06-21     0.0          -46.28  \n",
       "       2024-06-24     0.0          -92.56  \n",
       "       2024-06-25     0.0          138.84  \n",
       "\n",
       "[564 rows x 7 columns]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.positions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>type</th>\n",
       "      <th>symbol</th>\n",
       "      <th>entry_date</th>\n",
       "      <th>exit_date</th>\n",
       "      <th>entry</th>\n",
       "      <th>exit</th>\n",
       "      <th>shares</th>\n",
       "      <th>pnl</th>\n",
       "      <th>return_pct</th>\n",
       "      <th>agg_pnl</th>\n",
       "      <th>bars</th>\n",
       "      <th>pnl_per_bar</th>\n",
       "      <th>stop</th>\n",
       "      <th>mae</th>\n",
       "      <th>mfe</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>long</td>\n",
       "      <td>000001</td>\n",
       "      <td>2015-01-08</td>\n",
       "      <td>2015-01-13</td>\n",
       "      <td>15.24</td>\n",
       "      <td>14.76</td>\n",
       "      <td>1614</td>\n",
       "      <td>-774.72</td>\n",
       "      <td>-3.15</td>\n",
       "      <td>-774.72</td>\n",
       "      <td>3</td>\n",
       "      <td>-258.24</td>\n",
       "      <td>bar</td>\n",
       "      <td>-0.74</td>\n",
       "      <td>0.63</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>long</td>\n",
       "      <td>000001</td>\n",
       "      <td>2015-01-20</td>\n",
       "      <td>2015-01-23</td>\n",
       "      <td>13.81</td>\n",
       "      <td>14.46</td>\n",
       "      <td>1793</td>\n",
       "      <td>1165.45</td>\n",
       "      <td>4.71</td>\n",
       "      <td>390.73</td>\n",
       "      <td>3</td>\n",
       "      <td>388.48</td>\n",
       "      <td>bar</td>\n",
       "      <td>-0.25</td>\n",
       "      <td>0.79</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>long</td>\n",
       "      <td>000001</td>\n",
       "      <td>2015-02-09</td>\n",
       "      <td>2015-02-12</td>\n",
       "      <td>13.45</td>\n",
       "      <td>13.76</td>\n",
       "      <td>1857</td>\n",
       "      <td>575.67</td>\n",
       "      <td>2.30</td>\n",
       "      <td>966.40</td>\n",
       "      <td>3</td>\n",
       "      <td>191.89</td>\n",
       "      <td>bar</td>\n",
       "      <td>-0.23</td>\n",
       "      <td>0.40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>long</td>\n",
       "      <td>000001</td>\n",
       "      <td>2015-03-05</td>\n",
       "      <td>2015-03-10</td>\n",
       "      <td>13.41</td>\n",
       "      <td>13.94</td>\n",
       "      <td>1860</td>\n",
       "      <td>985.80</td>\n",
       "      <td>3.95</td>\n",
       "      <td>1952.20</td>\n",
       "      <td>3</td>\n",
       "      <td>328.60</td>\n",
       "      <td>bar</td>\n",
       "      <td>-0.12</td>\n",
       "      <td>0.93</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>long</td>\n",
       "      <td>000001</td>\n",
       "      <td>2015-04-14</td>\n",
       "      <td>2015-04-17</td>\n",
       "      <td>16.30</td>\n",
       "      <td>17.15</td>\n",
       "      <td>1540</td>\n",
       "      <td>1309.00</td>\n",
       "      <td>5.21</td>\n",
       "      <td>3261.20</td>\n",
       "      <td>3</td>\n",
       "      <td>436.33</td>\n",
       "      <td>bar</td>\n",
       "      <td>-0.20</td>\n",
       "      <td>0.98</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>184</th>\n",
       "      <td>long</td>\n",
       "      <td>000001</td>\n",
       "      <td>2024-04-08</td>\n",
       "      <td>2024-04-11</td>\n",
       "      <td>10.43</td>\n",
       "      <td>10.20</td>\n",
       "      <td>2269</td>\n",
       "      <td>-521.87</td>\n",
       "      <td>-2.21</td>\n",
       "      <td>-5546.18</td>\n",
       "      <td>3</td>\n",
       "      <td>-173.96</td>\n",
       "      <td>bar</td>\n",
       "      <td>-0.23</td>\n",
       "      <td>0.06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>185</th>\n",
       "      <td>long</td>\n",
       "      <td>000001</td>\n",
       "      <td>2024-04-12</td>\n",
       "      <td>2024-04-17</td>\n",
       "      <td>10.15</td>\n",
       "      <td>10.42</td>\n",
       "      <td>2303</td>\n",
       "      <td>621.81</td>\n",
       "      <td>2.66</td>\n",
       "      <td>-4924.37</td>\n",
       "      <td>3</td>\n",
       "      <td>207.27</td>\n",
       "      <td>bar</td>\n",
       "      <td>-0.11</td>\n",
       "      <td>0.27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>186</th>\n",
       "      <td>long</td>\n",
       "      <td>000001</td>\n",
       "      <td>2024-05-30</td>\n",
       "      <td>2024-06-04</td>\n",
       "      <td>11.20</td>\n",
       "      <td>10.98</td>\n",
       "      <td>2110</td>\n",
       "      <td>-464.20</td>\n",
       "      <td>-1.96</td>\n",
       "      <td>-5388.57</td>\n",
       "      <td>3</td>\n",
       "      <td>-154.73</td>\n",
       "      <td>bar</td>\n",
       "      <td>-0.29</td>\n",
       "      <td>0.14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>187</th>\n",
       "      <td>long</td>\n",
       "      <td>000001</td>\n",
       "      <td>2024-06-12</td>\n",
       "      <td>2024-06-17</td>\n",
       "      <td>10.84</td>\n",
       "      <td>10.12</td>\n",
       "      <td>2175</td>\n",
       "      <td>-1566.00</td>\n",
       "      <td>-6.64</td>\n",
       "      <td>-6954.57</td>\n",
       "      <td>3</td>\n",
       "      <td>-522.00</td>\n",
       "      <td>bar</td>\n",
       "      <td>-0.85</td>\n",
       "      <td>0.09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>188</th>\n",
       "      <td>long</td>\n",
       "      <td>000001</td>\n",
       "      <td>2024-06-21</td>\n",
       "      <td>2024-06-26</td>\n",
       "      <td>10.02</td>\n",
       "      <td>10.07</td>\n",
       "      <td>2314</td>\n",
       "      <td>115.70</td>\n",
       "      <td>0.50</td>\n",
       "      <td>-6838.87</td>\n",
       "      <td>3</td>\n",
       "      <td>38.57</td>\n",
       "      <td>bar</td>\n",
       "      <td>-0.14</td>\n",
       "      <td>0.11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>188 rows × 15 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     type  symbol entry_date  exit_date  entry   exit  shares      pnl  \\\n",
       "id                                                                       \n",
       "1    long  000001 2015-01-08 2015-01-13  15.24  14.76    1614  -774.72   \n",
       "2    long  000001 2015-01-20 2015-01-23  13.81  14.46    1793  1165.45   \n",
       "3    long  000001 2015-02-09 2015-02-12  13.45  13.76    1857   575.67   \n",
       "4    long  000001 2015-03-05 2015-03-10  13.41  13.94    1860   985.80   \n",
       "5    long  000001 2015-04-14 2015-04-17  16.30  17.15    1540  1309.00   \n",
       "..    ...     ...        ...        ...    ...    ...     ...      ...   \n",
       "184  long  000001 2024-04-08 2024-04-11  10.43  10.20    2269  -521.87   \n",
       "185  long  000001 2024-04-12 2024-04-17  10.15  10.42    2303   621.81   \n",
       "186  long  000001 2024-05-30 2024-06-04  11.20  10.98    2110  -464.20   \n",
       "187  long  000001 2024-06-12 2024-06-17  10.84  10.12    2175 -1566.00   \n",
       "188  long  000001 2024-06-21 2024-06-26  10.02  10.07    2314   115.70   \n",
       "\n",
       "     return_pct  agg_pnl  bars  pnl_per_bar stop   mae   mfe  \n",
       "id                                                            \n",
       "1         -3.15  -774.72     3      -258.24  bar -0.74  0.63  \n",
       "2          4.71   390.73     3       388.48  bar -0.25  0.79  \n",
       "3          2.30   966.40     3       191.89  bar -0.23  0.40  \n",
       "4          3.95  1952.20     3       328.60  bar -0.12  0.93  \n",
       "5          5.21  3261.20     3       436.33  bar -0.20  0.98  \n",
       "..          ...      ...   ...          ...  ...   ...   ...  \n",
       "184       -2.21 -5546.18     3      -173.96  bar -0.23  0.06  \n",
       "185        2.66 -4924.37     3       207.27  bar -0.11  0.27  \n",
       "186       -1.96 -5388.57     3      -154.73  bar -0.29  0.14  \n",
       "187       -6.64 -6954.57     3      -522.00  bar -0.85  0.09  \n",
       "188        0.50 -6838.87     3        38.57  bar -0.14  0.11  \n",
       "\n",
       "[188 rows x 15 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.trades"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>type</th>\n",
       "      <th>symbol</th>\n",
       "      <th>date</th>\n",
       "      <th>shares</th>\n",
       "      <th>limit_price</th>\n",
       "      <th>fill_price</th>\n",
       "      <th>fees</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>buy</td>\n",
       "      <td>000001</td>\n",
       "      <td>2015-01-08</td>\n",
       "      <td>1614</td>\n",
       "      <td>15.47</td>\n",
       "      <td>15.24</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>sell</td>\n",
       "      <td>000001</td>\n",
       "      <td>2015-01-13</td>\n",
       "      <td>1614</td>\n",
       "      <td>NaN</td>\n",
       "      <td>14.76</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>buy</td>\n",
       "      <td>000001</td>\n",
       "      <td>2015-01-20</td>\n",
       "      <td>1793</td>\n",
       "      <td>13.82</td>\n",
       "      <td>13.81</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>sell</td>\n",
       "      <td>000001</td>\n",
       "      <td>2015-01-23</td>\n",
       "      <td>1793</td>\n",
       "      <td>NaN</td>\n",
       "      <td>14.46</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>buy</td>\n",
       "      <td>000001</td>\n",
       "      <td>2015-02-09</td>\n",
       "      <td>1857</td>\n",
       "      <td>13.50</td>\n",
       "      <td>13.45</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>372</th>\n",
       "      <td>sell</td>\n",
       "      <td>000001</td>\n",
       "      <td>2024-06-04</td>\n",
       "      <td>2110</td>\n",
       "      <td>NaN</td>\n",
       "      <td>10.98</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>373</th>\n",
       "      <td>buy</td>\n",
       "      <td>000001</td>\n",
       "      <td>2024-06-12</td>\n",
       "      <td>2175</td>\n",
       "      <td>10.86</td>\n",
       "      <td>10.84</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>374</th>\n",
       "      <td>sell</td>\n",
       "      <td>000001</td>\n",
       "      <td>2024-06-17</td>\n",
       "      <td>2175</td>\n",
       "      <td>NaN</td>\n",
       "      <td>10.12</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>375</th>\n",
       "      <td>buy</td>\n",
       "      <td>000001</td>\n",
       "      <td>2024-06-21</td>\n",
       "      <td>2314</td>\n",
       "      <td>10.04</td>\n",
       "      <td>10.02</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>376</th>\n",
       "      <td>sell</td>\n",
       "      <td>000001</td>\n",
       "      <td>2024-06-26</td>\n",
       "      <td>2314</td>\n",
       "      <td>NaN</td>\n",
       "      <td>10.07</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>376 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     type  symbol       date  shares  limit_price  fill_price  fees\n",
       "id                                                                 \n",
       "1     buy  000001 2015-01-08    1614        15.47       15.24   0.0\n",
       "2    sell  000001 2015-01-13    1614          NaN       14.76   0.0\n",
       "3     buy  000001 2015-01-20    1793        13.82       13.81   0.0\n",
       "4    sell  000001 2015-01-23    1793          NaN       14.46   0.0\n",
       "5     buy  000001 2015-02-09    1857        13.50       13.45   0.0\n",
       "..    ...     ...        ...     ...          ...         ...   ...\n",
       "372  sell  000001 2024-06-04    2110          NaN       10.98   0.0\n",
       "373   buy  000001 2024-06-12    2175        10.86       10.84   0.0\n",
       "374  sell  000001 2024-06-17    2175          NaN       10.12   0.0\n",
       "375   buy  000001 2024-06-21    2314        10.04       10.02   0.0\n",
       "376  sell  000001 2024-06-26    2314          NaN       10.07   0.0\n",
       "\n",
       "[376 rows x 7 columns]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.orders"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>value</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>trade_count</td>\n",
       "      <td>1.880000e+02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>initial_market_value</td>\n",
       "      <td>1.000000e+05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>end_market_value</td>\n",
       "      <td>9.316113e+04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>total_pnl</td>\n",
       "      <td>-6.838870e+03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>unrealized_pnl</td>\n",
       "      <td>7.275958e-12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>total_return_pct</td>\n",
       "      <td>-6.838870e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>total_profit</td>\n",
       "      <td>5.291743e+04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>total_loss</td>\n",
       "      <td>-5.975630e+04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>total_fees</td>\n",
       "      <td>0.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>max_drawdown</td>\n",
       "      <td>-1.683067e+04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>max_drawdown_pct</td>\n",
       "      <td>-1.533078e+01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>win_rate</td>\n",
       "      <td>4.728261e+01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>loss_rate</td>\n",
       "      <td>5.271739e+01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>winning_trades</td>\n",
       "      <td>8.700000e+01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>losing_trades</td>\n",
       "      <td>9.700000e+01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>avg_pnl</td>\n",
       "      <td>-3.637697e+01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>avg_return_pct</td>\n",
       "      <td>-1.434043e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>avg_trade_bars</td>\n",
       "      <td>3.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>avg_profit</td>\n",
       "      <td>6.082463e+02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>avg_profit_pct</td>\n",
       "      <td>2.422069e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>avg_winning_trade_bars</td>\n",
       "      <td>3.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>avg_loss</td>\n",
       "      <td>-6.160443e+02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>avg_loss_pct</td>\n",
       "      <td>-2.450309e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>avg_losing_trade_bars</td>\n",
       "      <td>3.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>largest_win</td>\n",
       "      <td>3.246180e+03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>largest_win_pct</td>\n",
       "      <td>1.302000e+01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>largest_win_bars</td>\n",
       "      <td>3.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>largest_loss</td>\n",
       "      <td>-3.164840e+03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>largest_loss_pct</td>\n",
       "      <td>-1.262000e+01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>largest_loss_bars</td>\n",
       "      <td>3.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>max_wins</td>\n",
       "      <td>6.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>max_losses</td>\n",
       "      <td>6.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>sharpe</td>\n",
       "      <td>-1.241306e-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>sortino</td>\n",
       "      <td>-1.016597e-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>profit_factor</td>\n",
       "      <td>9.370517e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>ulcer_index</td>\n",
       "      <td>6.827702e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>36</th>\n",
       "      <td>upi</td>\n",
       "      <td>-4.096619e-03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>37</th>\n",
       "      <td>equity_r2</td>\n",
       "      <td>7.065747e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>38</th>\n",
       "      <td>std_error</td>\n",
       "      <td>4.894049e+03</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                      name         value\n",
       "0              trade_count  1.880000e+02\n",
       "1     initial_market_value  1.000000e+05\n",
       "2         end_market_value  9.316113e+04\n",
       "3                total_pnl -6.838870e+03\n",
       "4           unrealized_pnl  7.275958e-12\n",
       "5         total_return_pct -6.838870e+00\n",
       "6             total_profit  5.291743e+04\n",
       "7               total_loss -5.975630e+04\n",
       "8               total_fees  0.000000e+00\n",
       "9             max_drawdown -1.683067e+04\n",
       "10        max_drawdown_pct -1.533078e+01\n",
       "11                win_rate  4.728261e+01\n",
       "12               loss_rate  5.271739e+01\n",
       "13          winning_trades  8.700000e+01\n",
       "14           losing_trades  9.700000e+01\n",
       "15                 avg_pnl -3.637697e+01\n",
       "16          avg_return_pct -1.434043e-01\n",
       "17          avg_trade_bars  3.000000e+00\n",
       "18              avg_profit  6.082463e+02\n",
       "19          avg_profit_pct  2.422069e+00\n",
       "20  avg_winning_trade_bars  3.000000e+00\n",
       "21                avg_loss -6.160443e+02\n",
       "22            avg_loss_pct -2.450309e+00\n",
       "23   avg_losing_trade_bars  3.000000e+00\n",
       "24             largest_win  3.246180e+03\n",
       "25         largest_win_pct  1.302000e+01\n",
       "26        largest_win_bars  3.000000e+00\n",
       "27            largest_loss -3.164840e+03\n",
       "28        largest_loss_pct -1.262000e+01\n",
       "29       largest_loss_bars  3.000000e+00\n",
       "30                max_wins  6.000000e+00\n",
       "31              max_losses  6.000000e+00\n",
       "32                  sharpe -1.241306e-02\n",
       "33                 sortino -1.016597e-02\n",
       "34           profit_factor  9.370517e-01\n",
       "35             ulcer_index  6.827702e-01\n",
       "36                     upi -4.096619e-03\n",
       "37               equity_r2  7.065747e-01\n",
       "38               std_error  4.894049e+03"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.metrics_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Backtesting: 2015-01-01 00:00:00 to 2024-07-03 00:00:00\n",
      "\n",
      "Loading bar data...\n",
      "Loaded bar data: 0:00:00 \n",
      "\n",
      "Test split: 2015-01-05 00:00:00 to 2024-07-01 00:00:00\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  0% (0 of 450) |                        | Elapsed Time: 0:00:00 ETA:  --:--:--\n",
      " 73% (331 of 450) |################      | Elapsed Time: 0:00:00 ETA:   0:00:00\n",
      "100% (450 of 450) |######################| Elapsed Time: 0:00:00 Time:  0:00:00\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Finished backtest: 0:00:00\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>type</th>\n",
       "      <th>symbol</th>\n",
       "      <th>date</th>\n",
       "      <th>shares</th>\n",
       "      <th>limit_price</th>\n",
       "      <th>fill_price</th>\n",
       "      <th>fees</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>buy</td>\n",
       "      <td>000001</td>\n",
       "      <td>2015-01-19</td>\n",
       "      <td>1692</td>\n",
       "      <td>14.76</td>\n",
       "      <td>14.20</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>sell</td>\n",
       "      <td>000001</td>\n",
       "      <td>2015-02-09</td>\n",
       "      <td>1692</td>\n",
       "      <td>NaN</td>\n",
       "      <td>13.45</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>buy</td>\n",
       "      <td>000001</td>\n",
       "      <td>2015-05-18</td>\n",
       "      <td>1539</td>\n",
       "      <td>16.02</td>\n",
       "      <td>15.16</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>sell</td>\n",
       "      <td>000001</td>\n",
       "      <td>2015-06-08</td>\n",
       "      <td>1539</td>\n",
       "      <td>NaN</td>\n",
       "      <td>16.82</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>buy</td>\n",
       "      <td>000001</td>\n",
       "      <td>2015-06-29</td>\n",
       "      <td>1590</td>\n",
       "      <td>15.91</td>\n",
       "      <td>13.45</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>93</th>\n",
       "      <td>buy</td>\n",
       "      <td>000001</td>\n",
       "      <td>2023-12-18</td>\n",
       "      <td>2702</td>\n",
       "      <td>9.34</td>\n",
       "      <td>9.16</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>94</th>\n",
       "      <td>sell</td>\n",
       "      <td>000001</td>\n",
       "      <td>2024-01-15</td>\n",
       "      <td>2702</td>\n",
       "      <td>NaN</td>\n",
       "      <td>9.21</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>buy</td>\n",
       "      <td>000001</td>\n",
       "      <td>2024-04-15</td>\n",
       "      <td>2425</td>\n",
       "      <td>10.42</td>\n",
       "      <td>10.19</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>sell</td>\n",
       "      <td>000001</td>\n",
       "      <td>2024-05-06</td>\n",
       "      <td>2425</td>\n",
       "      <td>NaN</td>\n",
       "      <td>10.98</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>buy</td>\n",
       "      <td>000001</td>\n",
       "      <td>2024-06-17</td>\n",
       "      <td>2347</td>\n",
       "      <td>10.97</td>\n",
       "      <td>10.12</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>97 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    type  symbol       date  shares  limit_price  fill_price  fees\n",
       "id                                                                \n",
       "1    buy  000001 2015-01-19    1692        14.76       14.20   0.0\n",
       "2   sell  000001 2015-02-09    1692          NaN       13.45   0.0\n",
       "3    buy  000001 2015-05-18    1539        16.02       15.16   0.0\n",
       "4   sell  000001 2015-06-08    1539          NaN       16.82   0.0\n",
       "5    buy  000001 2015-06-29    1590        15.91       13.45   0.0\n",
       "..   ...     ...        ...     ...          ...         ...   ...\n",
       "93   buy  000001 2023-12-18    2702         9.34        9.16   0.0\n",
       "94  sell  000001 2024-01-15    2702          NaN        9.21   0.0\n",
       "95   buy  000001 2024-04-15    2425        10.42       10.19   0.0\n",
       "96  sell  000001 2024-05-06    2425          NaN       10.98   0.0\n",
       "97   buy  000001 2024-06-17    2347        10.97       10.12   0.0\n",
       "\n",
       "[97 rows x 7 columns]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result = strategy.backtest(days='mon')\n",
    "result.orders"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Abroker",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
